﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.DataAccess;
using System.Data.Objects.DataClasses;
using System.Data.Objects;
using Common.Model;

namespace Common.DataAccess
{
    public class GenericEntityReaderDataAccess<E, DC> : AbstractEntityDataAccess<DC>, IGenericEntityReaderDataAccess<E>
        where E : IObjectWithChangeTracker, new()
        where DC : ObjectContext, new()
    {

        #region IGenericEntityReaderDataAccess<E> Members

        protected virtual string GetEntitySet(E entity)
        {
            var entitySet = entity.GetType().Name;

            if (!entitySet.EndsWith("s"))
            {
                entitySet += "s";
            }
            return entitySet;
        }


        public virtual List<E> ReadEntities()
        {
            var list = ObjectContext.CreateQuery<E>(GetEntitySet(new E())).ToList();
            return list;
        }

        public virtual List<E> ReadEntitiesByParameter(IObjectWithChangeTracker entityObject)
        {
            var list = ObjectContext.CreateQuery<E>(GetEntitySet(new E())).ToList();
            return list;
        }

        #endregion

    }
}